clear
%% Problem
%   'EqualMinima','Himmelblau','Sixhump','ModifiedRastrigin','Vincent',
%   'Shubert','Composition','IncreasingMinima','Rastrigin','Schaffer'.
ProblemSet.FuncType = 'Vincent';
ProblemSet.dimension = 2;
% ProblemSet.k = [3,3,3];
% ProblemSet.BaseFunc = 3;

[d, x_bound, ObjFunc, OptSet, precision_init, precision, ~, MaximalBudget, FileName]...
    = problem_setting(ProblemSet);

epsilon = [0.1,0.01,0.001,0.0001];

%% problem
Problem.Dimension = d;
Problem.Domain = reshape(x_bound,[1,d*2]);
Problem.Sampling = @(n, region)Sampling_BoxConstraint(n, region, ObjFunc);
NewRegionNum = 2;

%% Algorithm
AlgorithmM.n0 = 4;
AlgorithmM.QuantileLevel = 0.3;
AlgorithmM.NewBudget = 3;
AlgorithmM.MaxSampleSize = 10;
AlgorithmM.StopCriteria = [1,MaximalBudget];
%% OPTIMIZATION
rep = 100;
TotalBudget = zeros(rep,length(epsilon));
OptNoFound = zeros(rep,length(epsilon));
Times = zeros(rep,length(epsilon));
%%
for iepsilon = 1:length(epsilon)
    OptSet.epsilon = epsilon(iepsilon);
    % problem
    MaxPartitionDepth_d = ceil(log((x_bound(2,:)-x_bound(1,:))./precision(iepsilon))./log(NewRegionNum));
    MaxPartitionDepth = sum(MaxPartitionDepth_d);
    Problem.Partition = @(Region,SampleSet)Partition_BoxConstraint(Region, NewRegionNum, MaxPartitionDepth, SampleSet);
    % algorithm
    ClearRadius0 = (x_bound(2,:)-x_bound(1,:))./(NewRegionNum.^(MaxPartitionDepth_d));
    ClearRadius = 2*min(ClearRadius0);
    AlgorithmM.radius = ClearRadius;
    for i = 1:rep
        disp(['epsilon: ',num2str(epsilon(iepsilon)),', replication: ',num2str(i),'.'])
        tic
        [ optima, ~, SampleSet, ~, OptSetRemain ] = prsmmo_test( Problem, AlgorithmM, OptSet );
        Times(i,iepsilon) = toc;
        disp(['time used: ',num2str(Times(i,iepsilon)),'.'])
        TotalBudget(i,iepsilon) = size(SampleSet,1);
        OptNoFound(i,iepsilon) = size(OptSetRemain,1);
    end
end
%%
clear i iepsilon OptSetRemain SampleSet optima
save(FileName)
a=[mean(TotalBudget)',var(TotalBudget)'];
